﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Diagnostics;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using Domain.Interface;
using Domain.Model;
using Domain.Model.Domain_Personal;
using Ds.DataBase;
using Ds.DataBase.Enum;
using Ds.DataBase.Model;
using Ds.DataBase.Model.PersonalAssess;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using xiangguangongzuo = Ds.DataBase.Model.xiangguangongzuo;
using zongjieone = Domain.Model.zongjieone;
using zongjietwo = Domain.Model.zongjietwo;

namespace Domain.RealiZation
{
    public class MyKaoHeBiaotmpl : IMyKaoHeBiao
    {
        public string CreateMyKaoHeBiao(string username, string sssj)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var 所属时间 = DateTime.Parse(sssj);
                var 用户信息 = db.JiaoZhiGongInfoContext.SingleOrDefault(s => s.Users.username == username);
                var 用户考核表 = 用户信息.kszw.MoBan_Main.ToList();
                var zw = db.ZhiWeiContext.ToList();
                dynamic result = new { };
                foreach (var keshizhiwu in 用户信息.kszw.zhiwu.PZhiWei.keshizhiwus)
                {
                    foreach (var keshizhiwuJiaoZhiGongInfo in keshizhiwu.JiaoZhiGongInfos)
                    {
                        if (!db.PersonalMainContext.Any(s =>
                            s.sssj.Year == 所属时间.Year && 所属时间.Month == s.sssj.Month &&
                            s.ssrid == keshizhiwuJiaoZhiGongInfo.Users.userinfoId))
                        {
                            result = new { code = 100, msg = "上级领导未生成考核表" };
                            return JsonConvert.SerializeObject(result);
                        }
                    }
                }
                foreach (var item in 用户考核表)
                {
                    if (!db.PersonalMainContext.Any(s => s.sssj.Year == 所属时间.Year && 所属时间.Month == s.sssj.Month && s.ssrid == 用户信息.Users.userinfoId && s.mobanid == item.mbid))
                    {

                        var pm = CreatePersonlMain(item, 所属时间, 用户信息);
                        foreach (var personalMain in pm)
                        {
                            db.PersonalMainContext.Add(personalMain);
                        }
                    }
                    else
                    {
                        result = new { code = 101, msg = "不可重复创建考核表！" };
                        return JsonConvert.SerializeObject(result);
                    }
                }

                db.SaveChanges();
                result = new { code = 1, msg = "创建考核表成功！" };
                return JsonConvert.SerializeObject(result);
            }
        }
        /// <summary>
        /// 创建个人考核表
        /// </summary>
        /// <param name="moBanMain"></param>
        /// <param name="所属时间"></param>
        /// <param name="用户信息"></param>
        /// <returns></returns>
        public List<PersonalMain> CreatePersonlMain(MoBan_Main moBanMain, DateTime 所属时间, JiaoZhiGongInfo 用户信息)
        {
            List<PersonalMain> lst = new List<PersonalMain>();
            PersonalMain pm = new PersonalMain();
            pm.sssj = 所属时间;
            if (pm.StateLog == null)
            {
                pm.StateLog = new List<PersonalMainStateLog>();
            }
            pm.StateLog.Add(new PersonalMainStateLog() { cjrid = 用户信息.Users.userinfoId, cjsj = DateTime.Now, PersonalState = 考核表状态.创建, isread = false, msg = "用户" + 用户信息.XingMing + "创建考核表" });
            pm.cjsj = DateTime.Now;
            pm.mobanid = moBanMain.mbid;
            pm.ssrid = 用户信息.Users.userinfoId;
            pm.state = 考核表状态.创建;
            pm.tableName = pm.sssj.Year + "年" + pm.sssj.Month + "月" + moBanMain.mbmc;
            #region 系统自带的任务

            foreach (var item in moBanMain.MoBan_Paths.OrderBy(s => s.shunxu))
            {
                var pmph = new PersonalPath();
                pmph.fenzhi = item.fenzhi;
                pmph.ControlID = item.ControlID;
                pmph.isEdit = item.isedit;
                pmph.ljmc = item.ljmc;
                pmph.mpid = item.ljid;
                pmph.paths = item.paths;
                pmph.shunxu = item.shunxu;
                pmph.isinclude = item.isinclude;
                if (item.isinclude)
                {
                    pmph.sonmobanid = item.SonMoBanMain.mbid;
                    pmph.sonmobanfenzhi = 0;
                    lst.AddRange(CreatePersonlMain(item.SonMoBanMain, 所属时间, 用户信息));
                }
                foreach (var item2 in item.MoBan_Contents.OrderBy(s => s.shunxu))
                {
                    var pmphpc = new PersonalContent();
                    pmphpc.fenzhi = item2.fenzhi;
                    pmphpc.isedit = false;
                    pmphpc.cjrid = 用户信息.Users.userinfoId;
                    pmphpc.shunxu = item2.shunxu;
                    pmphpc.cjsj = DateTime.Now;
                    pmphpc.gzcdrid = 用户信息.Users.userinfoId;
                    pmphpc.iswc = false;
                    //int monthDay = DateTime.DaysInMonth(dateTime.Year, dateTime.Month);当月一共多少天
                    pmphpc.jssj = DateTime.Now.AddDays(1 - DateTime.Now.Day).Date.AddMonths(1).AddSeconds(-1);
                    pmphpc.kssj = DateTime.Now.AddDays(1 - DateTime.Now.Day);
                    pmphpc.laiyuan = 来源.系统;
                    pmphpc.ljcs = 1;

                    pmphpc.nr = item2.nr;
                    pmphpc.PersonalOrigin = new PersonalOrigin() { pcid = 0, lyxingming = "系统", pmid = 0 };
                    foreach (var item3 in item2.MoBan_Answers.OrderBy(s => s.shunxu))
                    {
                        PersonalAnswer pmphpcpa = new PersonalAnswer();
                        pmphpcpa.fenzhi = item3.fenzhi;
                        pmphpcpa.html = item3.html;
                        pmphpcpa.shunxu = item3.shunxu;
                        pmphpcpa.tips = item3.tips;
                        if (pmphpc.PersonalAnswers == null)
                        {
                            pmphpc.PersonalAnswers = new List<PersonalAnswer>();
                        }
                        pmphpc.PersonalAnswers.Add(pmphpcpa);
                    }

                    if (pmph.PersonalContents == null)
                    {
                        pmph.PersonalContents = new List<PersonalContent>();
                    }
                    pmph.PersonalContents.Add(pmphpc);
                }

                if (pm.PersonalPath == null)
                {
                    pm.PersonalPath = new List<PersonalPath>();
                }
                pm.PersonalPath.Add(pmph);

            }

            #endregion
            #region 他人指派的任务
            var 他人指派的任务 = DesignatePath(所属时间, 用户信息);
            foreach (var item in pm.PersonalPath)
            {
                foreach (var itemm in 他人指派的任务.Where(s => pm.PersonalPath.Select(p => p.mpid).Contains(s.mpid)))
                {
                    if (item.mpid == itemm.mpid)
                    {
                        //item.isEdit = false;
                        foreach (var itemm2 in itemm.PersonalContents)
                        {
                            var 他人指派 = new PersonalContent();
                            他人指派.fenzhi = itemm2.fenzhi;
                            他人指派.isedit = false;
                            他人指派.cjrid = itemm2.cjrid;
                            他人指派.shunxu = itemm2.shunxu;
                            他人指派.nr = itemm2.nr;
                            他人指派.cjsj = DateTime.Now;
                            他人指派.gzcdrid = itemm2.gzcdrid;
                            他人指派.jssj = itemm2.jssj;
                            他人指派.kssj = itemm2.kssj;
                            他人指派.laiyuan = 来源.他人指派;
                            他人指派.ljcs = 1;
                            他人指派.PersonalAnswers = new List<PersonalAnswer>();
                            他人指派.PersonalOrigin = new PersonalOrigin() { pcid = itemm2.pcid, lyxingming = itemm2.cjrid.ToString(), pmid = 0 };
                            foreach (var itemm3 in itemm2.PersonalAnswers)
                            {
                                PersonalAnswer 他人指派Answer = new PersonalAnswer();
                                他人指派Answer.fenzhi = itemm3.fenzhi;
                                他人指派Answer.html = itemm3.html;
                                他人指派Answer.shunxu = itemm3.shunxu;
                                他人指派Answer.tips = itemm3.tips;
                                他人指派.PersonalAnswers.Add(他人指派Answer);
                            }

                            if (item.PersonalContents == null)
                            {
                                item.PersonalContents = new List<PersonalContent>();
                            }
                            item.PersonalContents.Add(他人指派);
                        }
                    }
                }
            }
            foreach (var itemm in 他人指派的任务.Where(s => !pm.PersonalPath.Select(p => p.mpid).Contains(s.mpid)))
            {
                var pp = new PersonalPath();
                //pp.isEdit = false;
                pp.fenzhi = itemm.fenzhi;
                pp.shunxu = itemm.shunxu;
                pp.paths = itemm.paths;
                pp.isinclude = itemm.isinclude;
                pp.ControlID = itemm.ControlID;
                pp.mpid = itemm.mpid;
                pp.paths = itemm.paths;
                pp.sonmobanid = itemm.sonmobanid;
                pp.ljmc = itemm.ljmc;
                pp.sonmobanfenzhi = itemm.sonmobanfenzhi;
                pp.PersonalContents = new List<PersonalContent>();
                foreach (var itemm2 in itemm.PersonalContents)
                {
                    var 他人指派 = new PersonalContent();
                    他人指派.fenzhi = itemm2.fenzhi;
                    他人指派.isedit = false;
                    他人指派.cjrid = itemm2.cjrid;
                    他人指派.shunxu = itemm2.shunxu;
                    他人指派.nr = itemm2.nr;
                    他人指派.cjsj = DateTime.Now;
                    他人指派.gzcdrid = itemm2.gzcdrid;
                    他人指派.jssj = itemm2.jssj;
                    他人指派.kssj = itemm2.kssj;
                    他人指派.laiyuan = 来源.他人指派;
                    他人指派.ljcs = 1;
                    他人指派.PersonalAnswers = new List<PersonalAnswer>();
                    他人指派.PersonalOrigin = new PersonalOrigin() { pcid = itemm2.pcid, lyxingming = itemm2.cjrid.ToString(), pmid = 0 };
                    foreach (var itemm3 in itemm2.PersonalAnswers)
                    {
                        PersonalAnswer 他人指派Answer = new PersonalAnswer();
                        他人指派Answer.fenzhi = itemm3.fenzhi;
                        他人指派Answer.html = itemm3.html;
                        他人指派Answer.shunxu = itemm3.shunxu;
                        他人指派Answer.tips = itemm3.tips;
                        他人指派.PersonalAnswers.Add(他人指派Answer);
                    }

                    pp.PersonalContents.Add(他人指派);
                }
                pm.PersonalPath.Add(pp);
            }
            #endregion
            #region 未完成的工作
            var 未完成的工作 = SurplusPath(所属时间, 用户信息);
            foreach (var item in pm.PersonalPath)
            {
                foreach (var itemm in 未完成的工作.Where(s => pm.PersonalPath.Select(p => p.mpid).Contains(s.mpid)))
                {
                    if (item.mpid == itemm.mpid)
                    {
                        //item.isEdit = false;
                        foreach (var itemm2 in itemm.PersonalContents)
                        {
                            var 未完成 = new PersonalContent();
                            未完成.fenzhi = itemm2.fenzhi;
                            未完成.isedit = false;
                            未完成.cjrid = itemm2.cjrid;
                            未完成.shunxu = itemm2.shunxu;
                            未完成.nr = itemm2.nr;
                            未完成.cjsj = DateTime.Now;
                            未完成.gzcdrid = itemm2.gzcdrid;
                            未完成.jssj = itemm2.jssj;
                            未完成.kssj = itemm2.kssj;
                            未完成.laiyuan = 来源.继承未完成;
                            未完成.ljcs = itemm2.ljcs + 1;
                            未完成.PersonalAnswers = new List<PersonalAnswer>();
                            未完成.PersonalOrigin = new PersonalOrigin() { pcid = itemm2.pcid, lyxingming = itemm2.cjrid.ToString(), pmid = 0 };
                            foreach (var itemm3 in itemm2.PersonalAnswers)
                            {
                                PersonalAnswer 未完成Answer = new PersonalAnswer();
                                未完成Answer.fenzhi = itemm3.fenzhi;
                                未完成Answer.html = itemm3.html;
                                未完成Answer.shunxu = itemm3.shunxu;
                                未完成Answer.tips = itemm3.tips;
                                未完成.PersonalAnswers.Add(未完成Answer);
                            }
                            item.PersonalContents.Add(未完成);
                        }
                    }
                }
            }
            foreach (var itemm in 未完成的工作.Where(s => !pm.PersonalPath.Select(p => p.mpid).Contains(s.mpid)))
            {
                var pp = new PersonalPath();
                pp.isEdit = false;
                pp.fenzhi = itemm.fenzhi;
                pp.shunxu = itemm.shunxu;
                pp.paths = itemm.paths;
                pp.isinclude = itemm.isinclude;
                pp.ControlID = itemm.ControlID;
                pp.mpid = itemm.mpid;
                pp.paths = itemm.paths;
                pp.sonmobanid = itemm.sonmobanid;
                pp.ljmc = itemm.ljmc;
                pp.sonmobanfenzhi = itemm.sonmobanfenzhi;
                pp.PersonalContents = new List<PersonalContent>();
                foreach (var itemm2 in itemm.PersonalContents)
                {
                    var 未完成 = new PersonalContent();
                    未完成.fenzhi = itemm2.fenzhi;
                    未完成.isedit = false;
                    未完成.cjrid = itemm2.cjrid;
                    未完成.shunxu = itemm2.shunxu;
                    未完成.nr = itemm2.nr;
                    未完成.cjsj = DateTime.Now;
                    未完成.gzcdrid = itemm2.gzcdrid;
                    未完成.jssj = itemm2.jssj;
                    未完成.kssj = itemm2.kssj;
                    未完成.laiyuan = 来源.继承未完成;
                    未完成.ljcs = itemm2.ljcs + 1;
                    未完成.PersonalAnswers = new List<PersonalAnswer>();
                    未完成.PersonalOrigin = new PersonalOrigin() { pcid = itemm2.pcid, lyxingming = itemm2.cjrid.ToString(), pmid = 0 };
                    foreach (var itemm3 in itemm2.PersonalAnswers)
                    {
                        PersonalAnswer 未完成Answer = new PersonalAnswer();
                        未完成Answer.fenzhi = itemm3.fenzhi;
                        未完成Answer.html = itemm3.html;
                        未完成Answer.shunxu = itemm3.shunxu;
                        未完成Answer.tips = itemm3.tips;
                        未完成.PersonalAnswers.Add(未完成Answer);
                    }
                    pp.PersonalContents.Add(未完成);
                }
                pm.PersonalPath.Add(pp);
            }
            #endregion
            lst.Add(pm);
            return lst;
        }
        /// <summary>
        /// 他人指派
        /// </summary>
        /// <param name="所属时间"></param>
        /// <param name="用户信息"></param>
        /// <returns></returns>
        private List<PersonalPath> DesignatePath(DateTime 所属时间, JiaoZhiGongInfo 用户信息)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                List<PersonalPath> lst = new List<PersonalPath>();
                var path = db.PersonalMainContext.Where(s => s.sssj.Year == 所属时间.Year && s.sssj.Month == 所属时间.Month && s.ssrid != 用户信息.Users.userinfoId)
                    .SelectMany(p => p.PersonalPath);


                foreach (var item in path)
                {
                    PersonalPath pp = new PersonalPath();
                    item.isEdit = false;
                    pp.mpid = item.mpid;
                    pp.shunxu = item.shunxu;
                    pp.paths = item.paths;
                    pp.isinclude = item.isinclude;
                    pp.ControlID = item.ControlID;
                    pp.fenzhi = item.fenzhi;
                    pp.ljmc = item.ljmc;
                    pp.sonmobanfenzhi = item.sonmobanfenzhi;
                    pp.sonmobanid = item.sonmobanid;
                    pp.PersonalContents = new List<PersonalContent>();
                    foreach (var item2 in item.PersonalContents)
                    {
                        var userinfoId = 用户信息.Users.userinfoId;
                        if (item2.iswc == false && item2.gzcdrid == userinfoId)
                        {
                            var pc = new PersonalContent();
                            pc.pcid = item2.pcid;
                            pc.shunxu = item2.shunxu;
                            pc.fenzhi = item2.fenzhi;
                            pc.isedit = false;
                            pc.gzcdrid = item2.gzcdrid;
                            pc.cjrid = item2.cjrid;
                            pc.nr = item2.nr;
                            pc.kssj = item2.kssj;
                            pc.jssj = item2.jssj;
                            pc.PersonalAnswers = new List<PersonalAnswer>();
                            foreach (var item3 in item2.PersonalAnswers)
                            {
                                var pa = new PersonalAnswer();
                                pa.fenzhi = item3.fenzhi;
                                pa.html = item3.html;
                                pa.shunxu = item3.shunxu;
                                pa.tips = item3.tips;
                                pc.PersonalAnswers.Add(pa);
                            }
                            pp.PersonalContents.Add(pc);
                        }

                    }
                    if (pp.PersonalContents != null && pp.PersonalContents.Count > 0)
                    {
                        if (lst.Count == 0)
                        {
                            lst.Add(pp);
                        }
                        else
                        {
                            foreach (var lst1 in lst)
                            {
                                var pppcids = lst1.PersonalContents.Select(p => p.pcid);
                                if (pp.PersonalContents.Where(s => pppcids.Contains(s.pcid)).Count() == 0)
                                {
                                    lst.Add(pp);
                                }
                            }
                        }

                    }
                }
                //db.SaveChanges();
                return lst;
            }
        }
        /// <summary>
        /// 上月未完成的工作
        /// </summary>
        /// <param name="所属时间"></param>
        /// <param name="用户信息"></param>
        /// <returns></returns>
        private List<PersonalPath> SurplusPath(DateTime 所属时间, JiaoZhiGongInfo 用户信息)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                List<PersonalPath> lst = new List<PersonalPath>();
                var 上月 = 所属时间.AddMonths(-1);
                var path = db.PersonalMainContext.Where(s => s.sssj.Year == 上月.Year && s.sssj.Month == 上月.Month).SelectMany(p => p.PersonalPath);

                PersonalPath pp = new PersonalPath();
                foreach (var item in path)
                {
                    item.isEdit = false;
                    pp.mpid = item.mpid;
                    pp.shunxu = item.shunxu;
                    pp.paths = item.paths;
                    pp.isinclude = item.isinclude;
                    pp.ControlID = item.ControlID;
                    pp.fenzhi = item.fenzhi;
                    pp.ljmc = item.ljmc;
                    pp.sonmobanfenzhi = item.sonmobanfenzhi;
                    pp.sonmobanid = item.sonmobanid;
                    pp.PersonalContents = new List<PersonalContent>();
                    foreach (var item2 in item.PersonalContents.Where(s => s.gzcdrid == 用户信息.Users.userinfoId && s.iswc == false && s.cjrid == 用户信息.Users.userinfoId))
                    {
                        var pc = new PersonalContent();
                        pc.shunxu = item2.shunxu;
                        pc.fenzhi = item2.fenzhi;
                        pc.isedit = false;
                        pc.gzcdrid = item2.gzcdrid;
                        pc.cjrid = item2.cjrid;
                        pc.nr = item2.nr;
                        pc.kssj = item2.kssj;
                        pc.jssj = item2.jssj;
                        foreach (var item3 in item2.PersonalAnswers)
                        {
                            var pa = new PersonalAnswer();
                            pa.fenzhi = item3.fenzhi;
                            pa.html = item3.html;
                            pa.shunxu = item3.shunxu;
                            pa.tips = item3.tips;
                            pc.PersonalAnswers.Add(pa);
                        }
                        pp.PersonalContents.Add(pc);
                    }
                    lst.Add(pp);
                }

                db.SaveChanges();
                return lst;
            }
        }
        public string GetMyKaoHeBiaos(string username)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var 用户信息 = db.JiaoZhiGongInfoContext.SingleOrDefault(s => s.Users.username == username);
                var data = db.PersonalMainContext.Where(s => s.ssrid == 用户信息.Users.userinfoId).OrderByDescending(s => s.sssj);
                List<PersonalMain> lst = new List<PersonalMain>();
                foreach (var item in data)
                {
                    PersonalMain pm = new PersonalMain();
                    pm.sssj = item.sssj;
                    pm.state = item.state;
                    pm.cjsj = item.cjsj;
                    pm.pmid = item.pmid;
                    pm.tableName = item.tableName;
                    pm.mobanid = item.mobanid;
                    lst.Add(pm);
                }
                return JsonConvert.SerializeObject(lst);
            }
        }

        public string GetGongZuoJiHuasBymbid(int mbid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.MyKaoHeBiaoContext.Where(s => s.mbid == mbid && s.khbzt == 考核表状态.提交审核);
                List<Domain_MyKaoHeBiao> lst = new List<Domain_MyKaoHeBiao>();
                foreach (var item in data)
                {
                    Domain_MyKaoHeBiao model = new Domain_MyKaoHeBiao();
                    model.cjsj = item.cjsj;
                    model.khbzt = (int)item.khbzt;
                    model.sssj = item.sssj;
                    model.mkhbid = item.mkhbid;
                    model.username = item.username;
                    lst.Add(model);
                }
                return JsonConvert.SerializeObject(lst);
            }
        }
        public string GetMyLiShiKaoHeBiaos(string username)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.MyKaoHeBiaoContext.Where(s => s.username == username && s.khbzt == 考核表状态.考核完成);
                List<Domain_MyKaoHeBiao> lst = new List<Domain_MyKaoHeBiao>();
                foreach (var item in data)
                {
                    Domain_MyKaoHeBiao model = new Domain_MyKaoHeBiao();
                    model.cjsj = item.cjsj;
                    model.khbzt = (int)item.khbzt;
                    model.sssj = item.sssj;
                    model.mkhbid = item.mkhbid;
                    model.username = item.username;
                    lst.Add(model);
                }
                return JsonConvert.SerializeObject(lst);
            }
        }

        public void UpdateMyKaoHeBiao(Domain_MyKaoHeBiao model)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.MyKaoHeBiaoContext.SingleOrDefault(s => s.mkhbid == model.mkhbid);
                foreach (var itemone in data.MyKaoHeBiaoOnes)
                {
                    foreach (var modelone in model.MyKaoHeBiaoOnes.Where(s =>
                        s.oneid == itemone.oneid && s.MyKaoHeBiaoTwos != null))
                    {
                        foreach (var datatwo in itemone.MyKaoHeBiaoTwos.ToArray())
                        {
                            if (modelone.MyKaoHeBiaoTwos == null || modelone.MyKaoHeBiaoTwos.Count == 0 || modelone.MyKaoHeBiaoTwos.All(s => s.twoid != datatwo.twoid))
                            {
                                db.Entry(datatwo).State = EntityState.Deleted;
                            }
                        }
                    }
                }
                db.SaveChanges();
                data = db.MyKaoHeBiaoContext.SingleOrDefault(s => s.mkhbid == model.mkhbid);
                var user_info = db.JiaoZhiGongInfoContext.SingleOrDefault(s => s.Users.username == data.username);
                foreach (var itemone in data.MyKaoHeBiaoOnes)
                {
                    foreach (var modelone in model.MyKaoHeBiaoOnes.Where(s => s.oneid == itemone.oneid && s.MyKaoHeBiaoTwos != null))
                    {
                        foreach (var modelitem in modelone.MyKaoHeBiaoTwos)
                        {
                            if (itemone.MyKaoHeBiaoTwos.Any(s => s.twoid == modelitem.twoid) && modelitem.twoid != 0)
                            {
                                var datatwo = itemone.MyKaoHeBiaoTwos.SingleOrDefault(s => s.twoid == modelitem.twoid);
                                datatwo.mbid = 0;
                                datatwo.bz = modelitem.bz;
                                datatwo.fz = modelitem.fz;
                                datatwo.gzjhkssj = modelitem.gzjhkssj;
                                datatwo.gzjhwcsj = modelitem.gzjhwcsj;
                                datatwo.gzjhwcsjstr = modelitem.gzjhwcsjstr;
                                datatwo.gzbz = modelitem.gzbz;
                                datatwo.gzjhwcqk = modelitem.gzjhwcqk;
                                datatwo.htmlstr = modelitem.htmlstr;
                                datatwo.mbnr = modelitem.mbnr;
                                datatwo.sx = modelitem.sx;
                                datatwo.zp = modelitem.zp;
                                datatwo.gzcdr = modelitem.gzcdr;
                                datatwo.gzcdrid = modelitem.gzcdrid;
                                datatwo.zycd = modelitem.zycd;
                                datatwo.zhou = modelitem.zhou;
                                datatwo.lyid = modelitem.lyid;
                                datatwo.lyxingming = modelitem.lyxingming;
                                datatwo.twolx = (MyKaoHeBiao_two.任务类型)modelitem.twolx;
                                datatwo.ljcs = modelitem.ljcs;
                                datatwo.Iswc = modelitem.Iswc;
                                //datatwo.xiangguangongzuoji = modelitem.xiangguangongzuoji;
                            }
                            else
                            {
                                MyKaoHeBiao_two datatwo = new MyKaoHeBiao_two();
                                datatwo.mbid = 0;
                                datatwo.MyKaoHeBiaoOne = itemone;
                                datatwo.bz = modelitem.bz;
                                datatwo.fz = modelitem.fz;
                                datatwo.gzjhkssj = modelitem.gzjhkssj;
                                datatwo.gzjhwcsj = modelitem.gzjhwcsj;
                                datatwo.gzjhwcsjstr = modelitem.gzjhwcsjstr;
                                datatwo.gzbz = modelitem.gzbz;
                                datatwo.gzjhwcqk = modelitem.gzjhwcqk;
                                datatwo.htmlstr = modelitem.htmlstr;
                                datatwo.mbnr = modelitem.mbnr;
                                datatwo.sx = modelitem.sx;
                                datatwo.IsEdit = true;
                                datatwo.zp = modelitem.zp;
                                datatwo.gzcdrid = modelitem.gzcdrid;
                                datatwo.gzcdr = modelitem.gzcdr;
                                datatwo.zycd = modelitem.zycd;
                                datatwo.zhou = modelitem.zhou;
                                datatwo.lyid = modelitem.lyid;
                                datatwo.lyxingming = modelitem.lyxingming;
                                datatwo.twolx = (MyKaoHeBiao_two.任务类型)modelitem.twolx;
                                datatwo.ljcs = modelitem.ljcs;
                                datatwo.Iswc = modelitem.Iswc;
                                itemone.MyKaoHeBiaoTwos.Add(datatwo);
                            }
                        }

                    }
                }
                db.SaveChanges();
            }
        }


        public void DelMyKaoHeBiao(int khbid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                PersonalMain myKaoHeBiao = db.PersonalMainContext.SingleOrDefault(s => s.pmid == khbid);
                if (myKaoHeBiao.state == 考核表状态.创建 || myKaoHeBiao.state == 考核表状态.编辑中)
                {
                    foreach (var item in myKaoHeBiao.PersonalPath.ToList())
                    {
                        foreach (var item2 in item.PersonalContents.ToList())
                        {
                            foreach (var item3 in item2.PersonalAnswers.ToList())
                            {
                                db.Entry(item3).State = EntityState.Deleted;
                            }
                            db.Entry(item2).State = EntityState.Deleted;
                        }
                        db.Entry(item).State = EntityState.Deleted;
                    }

                    foreach (var item in myKaoHeBiao.StateLog.ToList())
                    {
                        db.Entry(item).State = EntityState.Deleted;
                    }

                    db.Entry(myKaoHeBiao).State = EntityState.Deleted;
                    db.SaveChanges();
                }
            }
        }

        public string GetMyKaoHeBiaoInfo(int khbid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var model = Mapping.ToPersonalMain(db.PersonalMainContext.SingleOrDefault(s => s.pmid == khbid));
                return JsonConvert.SerializeObject(model);
            }
        }



        public void UpdateKaoHeBiaoZhuangTai(string username, int mkhbid, int v)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.MyKaoHeBiaoContext.SingleOrDefault(s => s.mkhbid == mkhbid);
                data.khbzt = (考核表状态)v;
                MyKaoHeBiao_TongGuoJiLu model = new MyKaoHeBiao_TongGuoJiLu();
                model.MyKaoHeBiao = data;
                model.cjsj = DateTime.Now;
                model.usernanme = username;
                model.ydsj = null;
                model.yijian = "通过";
                model.yjzt = false;
                if (data.MyKaoHeBiaoTongGuoJiLus == null)
                {
                    data.MyKaoHeBiaoTongGuoJiLus = new List<MyKaoHeBiao_TongGuoJiLu>();
                }
                data.MyKaoHeBiaoTongGuoJiLus.Add(model);
                db.SaveChanges();
            }
        }

        public string GetMyKaoHeBiaoBydatetime(string username, string datatimes)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var zw = db.ZhiWeiContext.ToList();
                List<int> nrids = db.KaoHeFanWeisContext.Where(s => s.username == username).Select(s => s.NeiRong.nrid).ToList();
                DateTime dtDateTime = DateTime.Parse(datatimes);
                var data = db.MyKaoHeBiaoContext.Where(s => s.MyKaoHeBiaoOnes.Any(p => nrids.Any(w => w == p.mbnrid)) && s.khbzt != 考核表状态.考核完成 && s.sssj.Year == dtDateTime.Year && s.sssj.Month == dtDateTime.Month);
                List<Domain_MyKaoHeBiao> lst = new List<Domain_MyKaoHeBiao>();
                foreach (var item in data)
                {
                    var tmpuser = db.JiaoZhiGongInfoContext.SingleOrDefault(s => s.Users.username == item.username);
                    if (tmpuser != null)
                    {
                        Domain_MyKaoHeBiao model = new Domain_MyKaoHeBiao();
                        model.cjsj = item.cjsj;
                        model.khbzt = (int)item.khbzt;
                        model.sssj = item.sssj;
                        model.mkhbid = item.mkhbid;
                        model.username = item.username;
                        model.ksmc = tmpuser.keshi.ksmc;
                        model.zwmc = tmpuser.kszw.zhiwu.zwmc;
                        model.xingming = tmpuser.XingMing;
                        lst.Add(model);
                    }

                }
                return JsonConvert.SerializeObject(lst);
            }
        }

        public string GetMyBoHuiYiJian(string username)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                string msg = string.Empty;
                var data = db.MyKaoHeBiaoContext.OrderByDescending(s => s.mkhbid).FirstOrDefault(s =>
                    s.username == username &&
                    s.MyKaoHeBiaoBoHuiYiJians.Any(p => p.yjzt == MyKaoHeBiao_BoHuiYiJian.意见状态.未读));
                if (data != null)
                {
                    var bhyj = data.MyKaoHeBiaoBoHuiYiJians
                        .FirstOrDefault(s => s.yjzt == MyKaoHeBiao_BoHuiYiJian.意见状态.未读);
                    if (bhyj != null)
                    {
                        var userinfo = db.JiaoZhiGongInfoContext.SingleOrDefault(s => s.Users.username == bhyj.usernanme);
                        msg = data.mkhbid + ":" + data.sssj.ToString("y") + " 工作计划(驳回意见)";
                        msg = msg + "：" + bhyj.yijian;
                        msg = msg + "-驳回人：" + userinfo.XingMing;
                    }

                }

                return msg;
            }
        }

        public void BoHuiYiJianYiYue(int mkhbid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.MyKaoHeBiaoContext.SingleOrDefault(s => s.mkhbid == mkhbid);
                var model = data.MyKaoHeBiaoBoHuiYiJians.FirstOrDefault(s => s.yjzt == MyKaoHeBiao_BoHuiYiJian.意见状态.未读);
                model.yjzt = MyKaoHeBiao_BoHuiYiJian.意见状态.已读;
                model.ydsj = DateTime.Now;
                db.SaveChanges();
            }
        }



        public void CreateLinShiGongZuo(string username, string gznr, string gzbz, int mkhbid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.MyKaoHeBiaoContext.SingleOrDefault(s => s.mkhbid == mkhbid);
                var one = data.MyKaoHeBiaoOnes.SingleOrDefault(s => s.mbnr == "临时工作");
                if (one == null)
                {
                    one = new MyKaoHeBiao_one();
                    one.fz = 0;
                    one.mbnr = "临时工作";
                    one.MyKaoHeBiao = data;
                    one.MyKaoHeBiaoTwos = new List<MyKaoHeBiao_two>();
                    one.htmlstr = "";
                    one.mbnrid = 0;
                    one.mbtype = 1;
                    one.sx = 3;
                }
                var two = new MyKaoHeBiao_two();
                two.fz = 0;
                two.IsEdit = true;
                two.MyKaoHeBiaoOne = one;
                two.gzbz = gzbz;
                two.gzjhkssj = DateTime.Now;
                two.gzjhwcqk = "";
                two.gzjhwcsj = DateTime.Now;
                two.htmlstr = "";
                two.mbid = 0;
                two.mbnr = gznr;
                two.sx = 1;
                two.zf = 0;
                two.zp = 0;
                one.MyKaoHeBiaoTwos.Add(two);
                data.MyKaoHeBiaoOnes.Add(one);
                db.SaveChanges();
            }
        }

        public void DelLinShiGongZuo(int mkhbid, int twoid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.MyKaoHeBiaoContext.SingleOrDefault(s => s.mkhbid == mkhbid);
                foreach (var item in data.MyKaoHeBiaoOnes)
                {
                    var two = item.MyKaoHeBiaoTwos.SingleOrDefault(s => s.twoid == twoid);
                    if (two != null)
                    {
                        db.Entry(two).State = EntityState.Deleted;
                        break;
                    }
                }
                db.SaveChanges();
            }
        }

        public void UploadFile(int mkhbid, int twoid, string fjmc, string path, string fileHash, string fileguid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.MyKaoHeBiaoContext.SingleOrDefault(s => s.mkhbid == mkhbid);
                foreach (var item in data.MyKaoHeBiaoOnes)
                {
                    var two = item.MyKaoHeBiaoTwos.SingleOrDefault(s => s.twoid == twoid);
                    if (two != null)
                    {
                        MyKaoHeBiao_twoFuJians twofujian = new MyKaoHeBiao_twoFuJians();
                        twofujian.MyKaoHeBiaoTwo = two;
                        twofujian.bz = "";
                        twofujian.fileHash = fileHash;
                        twofujian.fileguid = fileguid;
                        twofujian.fjmc = fjmc;
                        twofujian.path = path;
                        if (two.MyKaoHeBiaoTwoFuJianses == null)
                        {
                            two.MyKaoHeBiaoTwoFuJianses = new List<MyKaoHeBiao_twoFuJians>();
                        }
                        two.MyKaoHeBiaoTwoFuJianses.Add(twofujian);
                        break;
                    }
                }

                db.SaveChanges();
            }
        }

        public void DelUploadFile(int mkhbid, int twoid, int fjid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.MyKaoHeBiaoContext.SingleOrDefault(s => s.mkhbid == mkhbid);
                foreach (var item in data.MyKaoHeBiaoOnes)
                {
                    var two = item.MyKaoHeBiaoTwos.SingleOrDefault(s => s.twoid == twoid);
                    if (two != null)
                    {
                        var fj = two.MyKaoHeBiaoTwoFuJianses.SingleOrDefault(s => s.fjid == fjid);
                        db.Entry(fj).State = EntityState.Deleted;
                        break;
                    }
                }
                db.SaveChanges();
            }
        }

        public string GetFileList(int mkhbid, int twoid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.MyKaoHeBiaoContext.SingleOrDefault(s => s.mkhbid == mkhbid);
                var lst = new List<Domain_MyKaoHeBiaotwo_FuJians>();
                foreach (var item in data.MyKaoHeBiaoOnes)
                {
                    var two = item.MyKaoHeBiaoTwos.SingleOrDefault(s => s.twoid == twoid);

                    foreach (var item2 in two.MyKaoHeBiaoTwoFuJianses)
                    {
                        Domain_MyKaoHeBiaotwo_FuJians model = new Domain_MyKaoHeBiaotwo_FuJians();
                        model.bz = item2.bz;
                        model.fileHash = item2.fileHash;
                        model.fileguid = item2.fileguid;
                        model.fjid = item2.fjid;
                        model.fjmc = item2.fjmc;
                        model.path = item2.path;
                        lst.Add(model);
                    }
                    break;
                }
                return JsonConvert.SerializeObject(lst);
            }
        }

        //public void add考核总结内容(zongjietwo zongjietow, int zongjieoneid, string username)
        //{
        //    using (UserInfoClassContext db = new UserInfoClassContext())
        //    {
        //        var zongjieone = db.zongjieoneContext.SingleOrDefault(s => s.zjid == zongjieoneid);
        //        zongjieone.Zongjietwos.Add(new Ds.DataBase.Model.zongjietwo() { zongjielx = zongjietow.zongjielx, zongjielxstr = zongjietow.zongjielxstr, zongjienr = zongjietow.zongjienr });
        //        db.SaveChanges();
        //    }
        //}

        //public string get获取考核总结(string username, int mkhbid)
        //{
        //    using (UserInfoClassContext db = new UserInfoClassContext())
        //    {
        //        var 考核表 = db.MyKaoHeBiaoContext.SingleOrDefault(s => s.mkhbid == mkhbid && s.username == username);
        //        return JsonConvert.SerializeObject(考核表.MyKaoHeZongJie);
        //    }
        //}

        //public void update更新考核总结(int zongjietoid, zongjietwo zongjietow, string username)
        //{
        //    using (UserInfoClassContext db = new UserInfoClassContext())
        //    {
        //        var model = db.zongjietwoContext.SingleOrDefault(s => s.zjtowid == zongjietow.zjtowid);
        //        model.zongjielxstr = zongjietow.zongjielxstr;
        //        model.zongjielx = zongjietow.zongjielx;
        //        model.zongjienr = zongjietow.zongjienr;
        //        db.SaveChanges();
        //    }
        //}

        //public void del删除考核总结(int zongjietowid, string username)
        //{
        //    using (UserInfoClassContext db = new UserInfoClassContext())
        //    {
        //        var model = db.zongjietwoContext.SingleOrDefault(s => s.zjtowid == zongjietowid);
        //        db.Entry(model).State = EntityState.Deleted;
        //        db.SaveChanges();
        //    }
        //}


        public string 获取没有分配周数的月工作计划(int khbid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.MyKaoHeBiaoContext.SingleOrDefault(s => s.mkhbid == khbid);
                Domain_MyKaoHeBiao model = new Domain_MyKaoHeBiao();
                model.cjsj = data.cjsj;
                model.khbzt = (int)data.khbzt;
                model.sssj = data.sssj;
                model.mkhbid = data.mkhbid;
                model.username = data.username;
                model.xingming = db.JiaoZhiGongInfoContext.SingleOrDefault(s => s.Users.username == data.username)
                    .XingMing;
                model.mbid = data.mbid;
                model.MyKaoHeBiaoOnes = new List<Domain_MyKaoHeBiaoOne>();
                foreach (var item2 in data.MyKaoHeBiaoOnes)
                {
                    var model2 = new Domain_MyKaoHeBiaoOne();
                    model2.MyKaoHeBiaoTwos = new List<Domain_MyKaoHeBiaotwo>();
                    model2.fz = item2.fz;
                    model2.htmlstr = item2.htmlstr;
                    model2.mbnr = item2.mbnr;
                    model2.oneid = item2.oneid;
                    model2.sx = item2.sx;
                    model2.mbid = item2.mbnrid;
                    var gongzuojihua = db.GongZuoJiHuaContext.SingleOrDefault(s => s.mbid == data.mbid);
                    var one = gongzuojihua.GongZuoJiHuaNeiRongs.SingleOrDefault(p => p.nrid == item2.mbnrid);
                    model2.isedit = one == null || one.isedit;
                    foreach (var item3 in item2.MyKaoHeBiaoTwos.Where(s => s.zhou <= 0))
                    {
                        var model3 = new Domain_MyKaoHeBiaotwo();
                        model3.mbid = item3.mbid;
                        model3.bz = item3.bz;
                        model3.fz = item3.fz;
                        model3.gzjhkssj = item3.gzjhkssj;
                        model3.gzjhwcsj = item3.gzjhwcsj;
                        model3.gzjhwcsjstr = item3.gzjhwcsjstr;
                        model3.gzbz = item3.gzbz;
                        model3.gzjhwcqk = item3.gzjhwcqk;
                        model3.htmlstr = item3.htmlstr;
                        model3.mbnr = item3.mbnr;
                        model3.sx = item3.sx;
                        model3.zf = item3.zf;
                        model3.twoid = item3.twoid;
                        model3.IsEdit = item3.IsEdit;
                        model3.zp = item3.zp;
                        model3.gzcdr = item3.gzcdr;
                        model3.gzcdrid = item3.gzcdrid;
                        model3.zycd = item3.zycd;
                        model3.zhou = item3.zhou;
                        model3.lyid = item3.lyid;
                        model3.lyxingming = item3.lyxingming;
                        model3.twolx = (int)item3.twolx;
                        model3.ljcs = item3.ljcs;
                        model3.Iswc = item3.Iswc;
                        model3.MyKaoHeBiaotwoFuJianses = new List<Domain_MyKaoHeBiaotwo_FuJians>();
                        foreach (var item4 in item3.MyKaoHeBiaoTwoFuJianses)
                        {
                            var model4 = new Domain_MyKaoHeBiaotwo_FuJians();
                            model4.fjid = item4.fjid;
                            model4.bz = item4.bz;
                            model4.fileguid = item4.fileguid;
                            model4.fileHash = item4.fileHash;
                            model4.fjmc = item4.fjmc;
                            model4.path = item4.path;
                            model3.MyKaoHeBiaotwoFuJianses.Add(model4);
                        }
                        model2.MyKaoHeBiaoTwos.Add(model3);
                    }
                    model.MyKaoHeBiaoOnes.Add(model2);
                }

                return JsonConvert.SerializeObject(model);
            }
        }

        public string 获取已分配周数的周计划(int khbid, int 周数)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.MyKaoHeBiaoContext.SingleOrDefault(s => s.mkhbid == khbid);
                Domain_MyKaoHeBiao model = new Domain_MyKaoHeBiao();
                model.cjsj = data.cjsj;
                model.khbzt = (int)data.khbzt;
                model.sssj = data.sssj;
                model.mkhbid = data.mkhbid;
                model.username = data.username;
                model.xingming = db.JiaoZhiGongInfoContext.SingleOrDefault(s => s.Users.username == data.username)
                    .XingMing;
                model.mbid = data.mbid;
                model.MyKaoHeBiaoOnes = new List<Domain_MyKaoHeBiaoOne>();
                foreach (var item2 in data.MyKaoHeBiaoOnes)
                {
                    var model2 = new Domain_MyKaoHeBiaoOne();
                    model2.MyKaoHeBiaoTwos = new List<Domain_MyKaoHeBiaotwo>();
                    model2.fz = item2.fz;
                    model2.htmlstr = item2.htmlstr;
                    model2.mbnr = item2.mbnr;
                    model2.oneid = item2.oneid;
                    model2.sx = item2.sx;
                    model2.mbid = item2.mbnrid;
                    var gongzuojihua = db.GongZuoJiHuaContext.SingleOrDefault(s => s.mbid == data.mbid);
                    var one = gongzuojihua.GongZuoJiHuaNeiRongs.SingleOrDefault(p => p.nrid == item2.mbnrid);
                    model2.isedit = one == null || one.isedit;
                    foreach (var item3 in item2.MyKaoHeBiaoTwos.Where(s => s.zhou == 周数 || s.zhou <= 0))
                    {
                        var model3 = new Domain_MyKaoHeBiaotwo();
                        model3.mbid = item3.mbid;
                        model3.bz = item3.bz;
                        model3.fz = item3.fz;
                        model3.gzjhkssj = item3.gzjhkssj;
                        model3.gzjhwcsj = item3.gzjhwcsj;
                        model3.gzjhwcsjstr = item3.gzjhwcsjstr;
                        model3.gzbz = item3.gzbz;
                        model3.gzjhwcqk = item3.gzjhwcqk;
                        model3.htmlstr = item3.htmlstr;
                        model3.mbnr = item3.mbnr;
                        model3.sx = item3.sx;
                        model3.zf = item3.zf;
                        model3.twoid = item3.twoid;
                        model3.IsEdit = item3.IsEdit;
                        model3.zp = item3.zp;
                        model3.gzcdr = item3.gzcdr;
                        model3.gzcdrid = item3.gzcdrid;
                        model3.zycd = item3.zycd;
                        model3.zhou = item3.zhou;
                        model3.lyid = item3.lyid;
                        model3.lyxingming = item3.lyxingming;
                        model3.twolx = (int)item3.twolx;
                        model3.ljcs = item3.ljcs;
                        model3.Iswc = item3.Iswc;
                        model3.MyKaoHeBiaotwoFuJianses = new List<Domain_MyKaoHeBiaotwo_FuJians>();
                        foreach (var item4 in item3.MyKaoHeBiaoTwoFuJianses)
                        {
                            var model4 = new Domain_MyKaoHeBiaotwo_FuJians();
                            model4.fjid = item4.fjid;
                            model4.bz = item4.bz;
                            model4.fileguid = item4.fileguid;
                            model4.fileHash = item4.fileHash;
                            model4.fjmc = item4.fjmc;
                            model4.path = item4.path;
                            model3.MyKaoHeBiaotwoFuJianses.Add(model4);
                        }
                        model2.MyKaoHeBiaoTwos.Add(model3);
                    }
                    model.MyKaoHeBiaoOnes.Add(model2);
                }

                return JsonConvert.SerializeObject(model);
            }
        }

        public void 生成单条周计划(int khbid, int twoid, int 周数)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.MyKaoHeBiaoContext.SingleOrDefault(s => s.mkhbid == khbid);
                var flag = false;
                foreach (var item in data.MyKaoHeBiaoOnes)
                {
                    var item2 = item.MyKaoHeBiaoTwos.SingleOrDefault(s => s.twoid == twoid);
                    if (item2 != null)
                    {
                        flag = true;
                        item2.zhou = 周数;
                    }
                }
                if (!flag)
                {
                    throw new Exception("未找到此条任务");
                }
                db.SaveChanges();
            }
        }

        public void 取消单条周计划(int khbid, int twoid, int 周数)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.MyKaoHeBiaoContext.SingleOrDefault(s => s.mkhbid == khbid);
                var flag = false;
                foreach (var item in data.MyKaoHeBiaoOnes)
                {
                    var item2 = item.MyKaoHeBiaoTwos.SingleOrDefault(s => s.twoid == twoid);
                    if (item2 != null)
                    {
                        flag = true;
                        item2.zhou = -1;
                    }
                }
                if (!flag)
                {
                    throw new Exception("未找到此条任务");
                }
                db.SaveChanges();
            }
        }

        public void 更新工作计划完成情况(int khbid, int twoid, bool flag)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.MyKaoHeBiaoContext.Where(s => s.mkhbid == khbid).SelectMany(s => s.MyKaoHeBiaoOnes);
                foreach (var item in data)
                {
                    var two = item.MyKaoHeBiaoTwos.SingleOrDefault(s => s.twoid == twoid);
                    if (two != null)
                    {
                        two.Iswc = flag;
                    }
                }
                db.SaveChanges();
            }
        }

        public void 添加月工作总结(int khbid, int 总结类型, string 内容)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.ZongJieContext.FirstOrDefault(s => s.MyKaoHeBiaoId == khbid);
                if (data == null)
                {
                    data = new Ds.DataBase.Model.zongjieone();
                    data.MyKaoHeBiaoId = khbid;
                    db.ZongJieContext.Add(data);
                }
                data.zjmc = "月考核总结";
                if (data.Zongjietwos == null)
                {
                    data.Zongjietwos = new List<Ds.DataBase.Model.zongjietwo>();
                }
                data.Zongjietwos.Add(new Ds.DataBase.Model.zongjietwo() { zongjielx = 总结类型, zongjielxstr = ((Model.总结类型)总结类型).ToString(), zongjienr = 内容 });
                db.SaveChanges();
            }
        }

        public void 删除月工作总结(int khbid, int 总结Id)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.ZongJieContext.FirstOrDefault(s => s.MyKaoHeBiaoId == khbid);
                var datatow = data.Zongjietwos.First(s => s.zjtowid == 总结Id);
                db.Entry(datatow).State = EntityState.Deleted;
                db.SaveChanges();
            }

        }

        public void 修改月工作总结(int khbid, int 总结Id, string 内容, int 总结类型)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.ZongJieContext.First(s => s.MyKaoHeBiaoId == khbid).Zongjietwos.FirstOrDefault(s => s.zjtowid == 总结Id);
                data.zongjielxstr = ((Model.总结类型)总结类型).ToString();
                data.zongjielx = 总结类型;
                data.zongjienr = 内容;
                db.SaveChanges();
            }
        }

        public string 获取月工作总结(int khbid, int 总结Id)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.ZongJieContext.FirstOrDefault(s => s.MyKaoHeBiaoId == khbid).Zongjietwos.FirstOrDefault(s => s.zjtowid == 总结Id);
                return JsonConvert.SerializeObject(data);
            }
        }

        public string 获取月工作总结集(int khbid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.ZongJieContext.FirstOrDefault(s => s.MyKaoHeBiaoId == khbid);
                return JsonConvert.SerializeObject(data);
            }
        }

        public string UpdateMyKaoHeBiaoState(string username, int khbid, int state, string yijian = "")
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var 用户信息 = db.JiaoZhiGongInfoContext.SingleOrDefault(s => s.Users.username == username);
                var data = db.PersonalMainContext.SingleOrDefault(s => s.pmid == khbid);
                data.state = (考核表状态)state;
                dynamic result = new { };
                var msg = string.Empty;
                msg = 用户信息.XingMing + "变更状态为" + ((考核表状态)state).ToString();
                bool isread = true;
                if (!string.IsNullOrEmpty(yijian))
                {
                    msg = yijian;
                    isread = false;
                }

                data.StateLog.Add(new PersonalMainStateLog() { cjrid = 用户信息.Users.userinfoId, cjsj = DateTime.Now, isread = isread, msg = msg, PersonalState = ((考核表状态)state) });
                result = new { code = 200, msg = msg };
                db.SaveChanges();
                return JsonConvert.SerializeObject(result);
            }
        }

        public string CreateMyKaoHeBiaoContent(Domain_PersonalContent model, int mkhbid, int pathid, string username)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var 我的考核表 = db.PersonalMainContext.SingleOrDefault(s => s.pmid == mkhbid);
                var 考核表的模板 = db.Moban_MainContext.SingleOrDefault(s => s.mbid == 我的考核表.mobanid);
                var 我的考核表path = 我的考核表.PersonalPath.SingleOrDefault(s => s.ppid == pathid);
                var path默认的ControlID = 考核表的模板.MoBan_Paths.SingleOrDefault(s => s.ljid == 我的考核表path.mpid).ControlID;
                var 用户信息 = db.JiaoZhiGongInfoContext.SingleOrDefault(s => s.Users.username == username);
                var 下级子项分值 = 我的考核表path.PersonalContents.Sum(s => s.fenzhi);
                if (我的考核表path.fenzhi >= 下级子项分值 + model.fenzhi)
                {
                    PersonalContent pc = new PersonalContent();
                    pc.cjrid = 用户信息.Users.userinfoId;
                    pc.shunxu = model.shunxu;
                    pc.fenzhi = model.fenzhi;
                    pc.gzcdrid = model.gzcdrid;
                    pc.nr = model.nr;
                    pc.cjsj = DateTime.Now;
                    pc.isedit = true;
                    pc.iswc = false;
                    pc.jssj = model.jssj;
                    pc.kssj = DateTime.Now.AddDays(1 - DateTime.Now.Day);
                    pc.laiyuan = 来源.自己新建立;
                    pc.ljcs = 1;
                    pc.wcqk = string.Empty;
                    pc.PersonalOrigin = new PersonalOrigin();
                    //db.SaveChanges();//更新pcid
                    pc.PersonalAnswers = new List<PersonalAnswer>();
                    var controlhtml = db.MoBan_ControlContext.SingleOrDefault(s => s.id == path默认的ControlID).controlhtml;
                    //controlhtml = controlhtml.Replace("valueattr", "value=" + model.fenzhi);
                    //controlhtml = controlhtml.Replace("nameattr", "name=" + model.pcid);
                    pc.PersonalAnswers.Add(new PersonalAnswer() { fenzhi = model.fenzhi, shunxu = 1, tips = "", html = controlhtml });
                    我的考核表path.PersonalContents.Add(pc);
                    db.SaveChanges();
                    return pc.pcid.ToString();
                }
                else
                {
                    throw new Exception("分值超出设定值");
                }


            }
        }

        public string UpdateMyKaoHeBiaoContent(Domain_PersonalContent model, int mkhbid, int pathid, string username)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var 我的考核表 = db.PersonalMainContext.SingleOrDefault(s => s.pmid == mkhbid);
                var 考核表的模板 = db.Moban_MainContext.SingleOrDefault(s => s.mbid == 我的考核表.mobanid);
                var 我的考核表path = 我的考核表.PersonalPath.SingleOrDefault(s => s.ppid == pathid);
                var path默认的ControlID = 考核表的模板.MoBan_Paths.SingleOrDefault(s => s.ljid == 我的考核表path.mpid).ControlID;
                var 用户信息 = db.JiaoZhiGongInfoContext.SingleOrDefault(s => s.Users.username == username);
                PersonalContent pc = 我的考核表path.PersonalContents.SingleOrDefault(s => s.pcid == model.pcid);
                pc.cjrid = 用户信息.Users.userinfoId;
                pc.shunxu = model.shunxu;
                pc.fenzhi = model.fenzhi;
                pc.gzcdrid = model.gzcdrid;
                pc.jssj = model.jssj;
                pc.nr = model.nr;
                foreach (var item in pc.PersonalAnswers.ToList())
                {
                    db.Entry(item).State = EntityState.Deleted;
                }
                pc.PersonalAnswers = new List<PersonalAnswer>();
                var controlhtml = db.MoBan_ControlContext.SingleOrDefault(s => s.id == path默认的ControlID).controlhtml;
                //controlhtml = controlhtml.Replace("valueattr", "value=" + model.fenzhi);
                //controlhtml = controlhtml.Replace("nameattr", "name=" + model.pcid);
                pc.PersonalAnswers.Add(new PersonalAnswer() { fenzhi = model.fenzhi, shunxu = 1, tips = "", html = controlhtml });
                db.SaveChanges();
                return pc.pcid.ToString();
            }
        }

        public void DelMyKaoHeBiaoContent(int contentid, int mkhbid, int pathid, string username)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var 我的考核表 = db.PersonalMainContext.SingleOrDefault(s => s.pmid == mkhbid);
                var 考核表的模板 = db.Moban_MainContext.SingleOrDefault(s => s.mbid == 我的考核表.mobanid);
                var 我的考核表path = 我的考核表.PersonalPath.SingleOrDefault(s => s.ppid == pathid);
                var path默认的ControlID = 考核表的模板.MoBan_Paths.SingleOrDefault(s => s.ljid == 我的考核表path.mpid).ControlID;
                var 用户信息 = db.JiaoZhiGongInfoContext.SingleOrDefault(s => s.Users.username == username);
                PersonalContent pc = 我的考核表path.PersonalContents.SingleOrDefault(s => s.pcid == contentid);
                foreach (var item in pc.PersonalAnswers.ToList())
                {
                    db.Entry(item).State = EntityState.Deleted;
                }

                db.Entry(pc).State = EntityState.Deleted;
                db.SaveChanges();
            }
        }

        public string UpdateRelevantWork(int selfid, int RelevantWorkId, int mkhbid, int pathid, string username)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var 用户信息 = db.JiaoZhiGongInfoContext.SingleOrDefault(s => s.Users.username == username);
                var 我的考核表 = db.PersonalMainContext.SingleOrDefault(s => s.pmid == mkhbid);
                var 我的考核表path = 我的考核表.PersonalPath.SingleOrDefault(s => s.ppid == pathid);
                var 本身selfcontent = 我的考核表path.PersonalContents.SingleOrDefault(s => s.pcid == selfid);
                var 上级的工作RelevantWorkcontent = 我的考核表path.PersonalContents.SingleOrDefault(s => s.pcid == RelevantWorkId);
                if (上级的工作RelevantWorkcontent.RelevantWorks == null)
                {
                    上级的工作RelevantWorkcontent.RelevantWorks = new List<RelevantWork>();
                }

                var rw = new RelevantWork() { pcid = selfid };
                上级的工作RelevantWorkcontent.RelevantWorks.Add(rw);
                db.SaveChanges();
                return rw.xggzid.ToString();
            }
        }
        public void DelRelevantWork(int xggzid, int RelevantWorkId, int mkhbid, int pathid, string username)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var 用户信息 = db.JiaoZhiGongInfoContext.SingleOrDefault(s => s.Users.username == username);
                var 我的考核表 = db.PersonalMainContext.SingleOrDefault(s => s.pmid == mkhbid);
                var 我的考核表path = 我的考核表.PersonalPath.SingleOrDefault(s => s.ppid == pathid);
                var 上级的工作RelevantWorkcontent = 我的考核表path.PersonalContents.SingleOrDefault(s => s.pcid == RelevantWorkId);
                var 要取消关联的RelevantWork = 上级的工作RelevantWorkcontent.RelevantWorks.SingleOrDefault(s => s.xggzid == xggzid);
                上级的工作RelevantWorkcontent.RelevantWorks.Remove(要取消关联的RelevantWork);
                db.Entry(要取消关联的RelevantWork).State = EntityState.Deleted;
                db.SaveChanges();
            }
        }

        public List<dynamic> getGongZuoChengDanRen(string username)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var 人员s = db.JiaoZhiGongInfoContext.Where(s => s.Users.username != username);
                List<dynamic> lst = new List<dynamic>();
                foreach (var item in 人员s)
                {
                    dynamic d = new { name = item.XingMing + ":" + item.keshi.ksmc + ":" + item.Users.username, id = item.Users.userinfoId };
                    lst.Add(d);
                }

                return lst;
            }
        }
    }
}
